package com.example.sangong.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.example.sangong.demo.service.WebSocketServer;
import com.example.sangong.entity.PersonExport;
import com.example.sangong.entity.PersonImport;
import com.example.sangong.http.Result;
import com.example.sangong.myannotation.SysLog;
import com.example.sangong.req.Req;
import com.example.sangong.req.person.PersonAddReq;
import com.example.sangong.req.person.PersonInfoReq;
import com.example.sangong.req.person.PersonQueryReq;
import com.example.sangong.req.person.PersonUpdateReq;
import com.example.sangong.service.PersonService;
import com.example.sangong.utils.MyExcelUtils;
import com.example.sangong.websocket.WebSocketMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Api(tags = "人员")
@RestController
@RequestMapping("/person")

public class PersonController {
    @Autowired
    private PersonService personService;
    @Resource
    private WebSocketServer webSocketServer;

    @ApiOperation("根据id查询人员信息")
    @PostMapping("/selectPersonById")
    public Result selectPersonById(@RequestBody PersonInfoReq req) {
        return Result.success(personService.selectPersonById(req));
    }

    @PostMapping("/addPerson")
    @SysLog("新增用户信息")
    @ApiOperation("新增用户信息")
    public Result addPerson(@RequestBody Req<PersonAddReq> req) {
        personService.addPerson(req.getReq());
        return Result.success(Boolean.TRUE);
    }

    @PostMapping("/updatePerson")
    @SysLog("修改用户信息")
    @ApiOperation("修改用户信息")
    public Result updatePerson(@RequestBody Req<PersonUpdateReq> req) {
        personService.updatePerson(req.getReq());
        return Result.success(Boolean.TRUE);
    }

    @PostMapping("/selectByName")
    public Result selectByName(@RequestBody PersonQueryReq req) {
        return Result.success(personService.selectByName(req));
    }

    @GetMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        return Result.success(personService.deleteById(id));
    }

    @PostMapping("/getUserPage")
    @SysLog("分页查询用户信息")
    @ApiOperation("分页查询用户信息")
    public Result getUserPage(@RequestBody Req<PersonQueryReq> req) {
        return Result.success(personService.getUserPage(req.getReq()));
    }

    @GetMapping("/selectPageCustom")
    public Result selectPageCustom(PersonQueryReq req) {
        return Result.success(personService.selectPageCustom(req));
    }

    @GetMapping("/test")
    public Result test() {
        return Result.success("");
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response, String name) {
        String fileName = "人员列表";
        if (StrUtil.isNotBlank(name)) {
            fileName += "-" + name;
        }
        fileName += DateUtil.format(new Date(), "yyyyMMddHHmmss");
        MyExcelUtils.writeData(response, personService.getExportPersonList(name), PersonExport.class, fileName);

    }

    @PostMapping("/importPerson")
    public Result importPerson(@RequestPart(value = "file") MultipartFile file) {
        return Result.success(personService.importData(MyExcelUtils.readData(file, PersonImport.class)));
    }

    @PostMapping("/sendMessage")
    public Result sendMessage(@RequestBody WebSocketMessage webSocketMessage) {
        webSocketServer.sendMessage(webSocketMessage);
        return Result.success(Boolean.TRUE);
    }


}
